﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lib.Web.Mvc;
using System.IO;
using Lib.Web.Mvc.JQuery.TreeView;

namespace jQuery.Treeview.Controllers
{
    public class HomeController : Controller
    {
        #region Actions
        public ActionResult Treeview()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FileSystemInfos(string root)
        {
            int? nodeId = (root == "source") ? (int?)null : Convert.ToInt32(root);
            IEnumerable<FileSystemInfo> children = FileSystemInfosRepository.GetFileSystemInfos(nodeId);

            List<TreeViewNode> nodes = new List<TreeViewNode>();
            foreach (FileSystemInfo child in children)
            {
                bool leaf = child is FileInfo;
                nodes.Add(new TreeViewNode()
                {
                    id = Convert.ToString(FileSystemInfosRepository.GetNodeId(child)),
                    text = child.Name,
                    classes = leaf ? "file" : "folder",
                    hasChildren = !leaf
                });
            }

            return Json(nodes);
        }
        #endregion
    }
}
